home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / Papers / aSEPiA example source / Application / source / CTextDocument.cp < prev    next >
Encoding:
Text File  |  1999-06-25  |  8.2 KB  |  319 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CTextDocument.cp                ©1996-1998 Metrowerks Inc. All rights reserved.
  3. // =================================================================================
  4.  
  5. #include <LFile.h>
  6. #include <LPlaceHolder.h>
  7. #include <LPrintout.h>
  8. #include <LString.h>
  9. #include <LWindow.h>
  10. #include <PP_Messages.h>
  11. #include <UMemoryMgr.h>
  12. #include <UWindows.h>
  13.  
  14. #include "PrintingConstants.h"
  15. #include "CTextView.h"
  16.  
  17. #include "CTextDocument.h"
  18. #include "CPluginManager.h"
  19. #include <string.h>
  20.  
  21. // ---------------------------------------------------------------------------------
  22. //        • CTextDocument(LCommander*, FSSpec*)
  23. // ---------------------------------------------------------------------------------
  24.  
  25.  
  26. CTextDocument::CTextDocument(
  27.     LCommander    *inSuper,
  28.     FSSpec        *inFileSpec )
  29.         : LSingleDoc( inSuper )
  30. {
  31.     // Create window for our document.
  32.     mWindow = nil;
  33.     mWindow = PP_PowerPlant::LWindow::CreateWindow( PPob_TextWindow, this );
  34.     ThrowIfNil_(mWindow);
  35.     
  36.     // Specify that the text view should be the
  37.     // target when the window is activated.
  38.     mTextView = dynamic_cast<CTextView *>(mWindow->FindPaneByID( kTextView ));
  39.     ThrowIfNil_(mTextView);
  40.     mWindow->SetLatentSub( mTextView );
  41.     
  42.     if ( inFileSpec == nil ) {
  43.  
  44.         // Set the name of new window.
  45.         NameNewDoc();
  46.         DumpPluginData();
  47.         
  48.     } else {
  49.     
  50.         // Display contents of file in window.
  51.         OpenFile( *inFileSpec );
  52.     
  53.     }
  54.     
  55.     // Make the window visible.
  56.     mWindow->Show();
  57. }
  58.  
  59.  
  60. // ---------------------------------------------------------------------------------
  61. //        • NameNewDoc
  62. // ---------------------------------------------------------------------------------
  63.  
  64. void
  65. CTextDocument::NameNewDoc()
  66. {
  67.     // Setup the window title. Start with the default title.
  68.     PP_PowerPlant::LStr255    theTitle( "\pUntitled" );
  69.  
  70.     // Find the first available title. We could also check the window
  71.     // pane id if we wanted to make sure we didn't collide with other
  72.     // window types.
  73.     SInt32    theNumber = 0;
  74.     while ( PP_PowerPlant::UWindows::FindNamedWindow( theTitle ) != nil ) {
  75.  
  76.         // An existing window has the current name
  77.         // Increment counter and try again.
  78.         ++theNumber;
  79.         theTitle = "\pUntitled ";
  80.         theTitle += static_cast<SInt32>(theNumber);
  81.  
  82.     }        
  83.     
  84.     // Finally, set window title.
  85.     mWindow->SetDescriptor( theTitle );
  86. }
  87.  
  88.  
  89. // ---------------------------------------------------------------------------------
  90. //        • OpenFile
  91. // ---------------------------------------------------------------------------------
  92.  
  93. void
  94. CTextDocument::OpenFile(
  95.     FSSpec    &inFileSpec )
  96. {
  97.     mFile = nil;
  98.     
  99.     // Create a new file object.
  100.     PP_PowerPlant::StDeleter<PP_PowerPlant::LFile>
  101.                     theFile( new PP_PowerPlant::LFile( inFileSpec ) );
  102.     
  103.     // Open the data fork.
  104.     theFile->OpenDataFork( fsRdWrPerm );
  105.     
  106.     // Read the entire file contents and close the file.
  107.     PP_PowerPlant::StHandleBlock    theTextH(theFile->ReadDataFork());
  108.     theFile->CloseDataFork();
  109.     
  110.     // Put the contents in the text view
  111.     // and clear the dirty flag.
  112.     mTextView->SetTextHandle( theTextH );
  113.     mTextView->SetDirty( false );
  114.             
  115.     // Set the window title to the name of the file and
  116.     // flag that the document has an associated file.
  117.     mWindow->SetDescriptor( inFileSpec.name );
  118.     mIsSpecified = true;
  119.  
  120.     mFile = theFile.Release();
  121. }
  122.  
  123.  
  124. // ---------------------------------------------------------------------------------
  125. //        • IsModified
  126. // ---------------------------------------------------------------------------------
  127.  
  128. Boolean
  129. CTextDocument::IsModified()
  130. {
  131.     // Document has changed if the text view is dirty.
  132.     mIsModified = mTextView->IsDirty();
  133.     return mIsModified;
  134. }
  135.  
  136.  
  137. // ---------------------------------------------------------------------------------
  138. //        • DoAESave
  139. // ---------------------------------------------------------------------------------
  140.  
  141. void
  142. CTextDocument::DoAESave(
  143.     FSSpec    &inFileSpec,
  144.     OSType    inFileType )
  145. {
  146.     // Delete the existing file object.
  147.     // Note: this does nothing to the actual file on disk.
  148.     delete mFile;
  149.     mFile = nil;
  150.     
  151.     // Make a new file object.
  152.     mFile = new PP_PowerPlant::LFile( inFileSpec );
  153.     
  154.     // Get the proper file type.
  155.     OSType    theFileType = 'TEXT';
  156.     if ( inFileType != PP_PowerPlant::fileType_Default ) {
  157.         theFileType = inFileType;
  158.     }
  159.     
  160.     // Make new file on disk (we'll use
  161.     // SimpleText's creator for this example).
  162.     mFile->CreateNewDataFile( 'ttxt', theFileType );
  163.     
  164.     // Write out the data.
  165.     DoSave();
  166.  
  167.     // Change window title to reflect the new name.
  168.     mWindow->SetDescriptor( inFileSpec.name );
  169.  
  170.     // Document now has a specified file.
  171.     mIsSpecified = true;
  172. }
  173.  
  174.  
  175. // ---------------------------------------------------------------------------------
  176. //        • DoSave
  177. // ---------------------------------------------------------------------------------
  178.  
  179. void
  180. CTextDocument::DoSave()
  181. {
  182.     // Open the data fork.
  183.     mFile->OpenDataFork( fsRdWrPerm );
  184.  
  185.     // Get the text from the text view.
  186.     Handle    theTextH = mTextView->GetTextHandle();
  187.     
  188.     // Lock the text handle.
  189.     PP_PowerPlant::StHandleLocker    theLock( theTextH );
  190.     
  191.     // Write the text to the file.
  192.     mFile->WriteDataFork( *theTextH, ::GetHandleSize( theTextH ) );
  193.  
  194.     // Close the data fork.
  195.     mFile->CloseDataFork();
  196.  
  197.     // Saving makes doc un-dirty.
  198.     mTextView->SetDirty( false );
  199. }
  200.  
  201.  
  202. // ---------------------------------------------------------------------------------
  203. //        • DoRevert
  204. // ---------------------------------------------------------------------------------
  205.  
  206. void
  207. CTextDocument::DoRevert()
  208. {
  209.     // Open the data fork.
  210.     mFile->OpenDataFork( fsRdWrPerm );
  211.  
  212.     // Read the entire file contents and close the file.
  213.     Handle    theTextH = mFile->ReadDataFork();
  214.     mFile->CloseDataFork();
  215.     
  216.     // Put the contents in the text view
  217.     // and clear the dirty flag.
  218.     mTextView->SetTextHandle( theTextH );
  219.     mTextView->SetDirty( false );
  220.     
  221.     // Dispose of the text.
  222.     ::DisposeHandle( theTextH );
  223.  
  224.     // Refresh the text view.
  225.     mTextView->Refresh();
  226. }
  227.  
  228.  
  229. // ---------------------------------------------------------------------------------
  230. //        • DoPrint
  231. // ---------------------------------------------------------------------------------
  232.  
  233. void
  234. CTextDocument::DoPrint()
  235. {
  236.     // Create the printout.
  237.     PP_PowerPlant::StDeleter<PP_PowerPlant::LPrintout>
  238.                         thePrintout(PP_PowerPlant::LPrintout::CreatePrintout( PPob_TextPrintout ));
  239.     ThrowIfNil_(thePrintout.Get());
  240.     
  241.     // Set the print record.
  242.     
  243.     if (mPrintRecordH) {
  244.         thePrintout->SetPrintRecord( mPrintRecordH );
  245.     }
  246.     
  247.     // Get the text placeholder.
  248.     PP_PowerPlant::LPlaceHolder    *thePlaceholder;
  249.     thePlaceholder = dynamic_cast<PP_PowerPlant::LPlaceHolder*>
  250.                             (thePrintout->FindPaneByID( kTextPlaceholder ));
  251.     ThrowIfNil_(thePlaceholder);
  252.     
  253.     // Install the text view in the placeholder.
  254.     thePlaceholder->InstallOccupant( mTextView, atNone );
  255.     
  256.     // Set the frame size.
  257.     SetPrintFrameSize();
  258.     
  259.     // Print.
  260.     thePrintout->DoPrintJob();
  261.     
  262.     // Delete the printout (handled automatically by the
  263.     // StDeleter object). The text view is returned
  264.     // to the window when the placeholder is destroyed.
  265.  
  266. }
  267.  
  268.  
  269. // ---------------------------------------------------------------------------------
  270. //        • SetPrintFrameSize
  271. // ---------------------------------------------------------------------------------
  272.  
  273. void
  274. CTextDocument::SetPrintFrameSize()
  275. {
  276.     // Get the frame size.
  277.     PP_PowerPlant::SDimension16    theFrameSize;
  278.     mTextView->GetFrameSize( theFrameSize );
  279.     
  280.     // Get the text edit record handle.
  281.     TEHandle    theTextEditH = mTextView->GetMacTEH();
  282.     
  283.     // Calculate the number of lines per page.
  284.     SInt16    theLinesPerPage;
  285.     theLinesPerPage = theFrameSize.height / (**theTextEditH).lineHeight;
  286.  
  287.     // Resize the frame to an integral number of lines.
  288.     mTextView->ResizeFrameTo( theFrameSize.width,
  289.         (**theTextEditH).lineHeight * theLinesPerPage, false );
  290.  
  291. }
  292.  
  293.  
  294. // ---------------------------------------------------------------------------
  295. //    • GetFileType                                                      [public]
  296. // ---------------------------------------------------------------------------
  297. //    Return the type (four character code) of the file used for saving
  298. //    the Document. Subclasses should override if they support saving files.
  299.  
  300. OSType
  301. CTextDocument::GetFileType() const
  302. {
  303.     return FOUR_CHAR_CODE('TEXT');
  304. }
  305.  
  306. void CTextDocument::DumpPluginData()
  307. {
  308.     CPluginManager    tempPM;
  309.     
  310.     short count = tempPM.GetPluginCount();
  311.     
  312.     for( short i = 1; i <= count; i++ )
  313.     {
  314.         char    name[256];
  315.         tempPM.GetPluginName( i, name );
  316.         mTextView->Insert( name, strlen(name) );
  317.     }
  318.         
  319. }